package com.lai.note.controller.admin;

import com.lai.note.dto.MessageBean;
import com.lai.note.model.User;
import com.lai.note.service.CopyWritingService;
import com.lai.note.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/admin/copyWriting")
@Validated

public class AdminCopyWritingAction {

    @Autowired
    private CopyWritingService copyWritingService;

    @RequestMapping(value = "/addNew.action",method = RequestMethod.POST)
    public MessageBean userAddNew(HttpSession session, @NotNull(message = "文案内容不能为空") String content){
        User user = (User) session.getAttribute("user");
        if(copyWritingService.adminAddNew(user.getUserId(),content))
            return MessageUtil.success();
        return MessageUtil.failure();
    }

    @RequestMapping(value = "/delete.action",method = RequestMethod.DELETE)
    public MessageBean deleteCopyWriting(@NotNull(message = "id不能为空") int writeId){
        if (copyWritingService.adminDeleteCopyWriting(writeId)){
            return MessageUtil.success();
        }
        return MessageUtil.failure();
    }
}
